package org.nf.ssm.web.controller.notice;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.nf.ssm.common.ResultVO;
import org.nf.ssm.domain.Notice;
import org.nf.ssm.service.NoticeService;
import org.nf.ssm.web.controller.BaseController;
import org.nf.ssm.web.vo.PageVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhu
 * @date 2024-10-15
 */
@RestController
@RequiredArgsConstructor
public class NoticeController extends BaseController {

    private final NoticeService noticeService;

    @GetMapping("/notice/getNotice")
    public ResultVO getNotice(@RequestParam("adminId")@RequestBody Integer adminId) throws JsonProcessingException {
        List<Notice> noticeList = noticeService.getNotice(adminId);
        String json = new ObjectMapper().writeValueAsString(noticeList);
        return success(json);
    }

    @GetMapping("/notice/getAllNotice")
    public PageVO getAllNotice(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize) {
        PageInfo<Notice> noticeList = noticeService.getAllNotice(pageNum, pageSize);
        PageVO pageVO = new PageVO();
        pageVO.setTotal(noticeList.getTotal());
        pageVO.setPageNum(noticeList.getPageNum());
        pageVO.setPageSize(noticeList.getPageSize());
        pageVO.setCode(200);
        pageVO.setData(noticeList);
        return pageVO;
    }

    @PostMapping("/notice/addNotice")
    public ResultVO addNotice(@RequestBody Notice notice) {
        noticeService.addNotice(notice);
        return success();
    }

    @PostMapping("/notice/updateNotice")
    public ResultVO updateNotice(@RequestBody Notice notice) {
        noticeService.updateNotice(notice);
        return success();
    }

    @PostMapping("/notice/deleteNotice")
    public ResultVO deleteNotice(Integer noticeId) {
        noticeService.deleteNotice(noticeId);
        return success();
    }

    @GetMapping("/notice/getNoticeByTheme")
    public ResultVO getNoticeByTheme(@RequestParam("theme") String theme) {
        Notice notice = noticeService.getNoticeByTheme(theme);
        return success(notice);
    }
}
